package com.whcoding.helloworld.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Manager
 */
@RestController
@RequestMapping("/hello")
public class HelloWorldController {
	private static final Logger log = LoggerFactory.getLogger(HelloWorldController.class);

	/**
	 * Hello，World
	 * http://localhost:9170/demo/hello?name=111
	 * <p>
	 * localhost:9170/demo/hello
	 *
	 * @param name 参数，非必须
	 * @return Hello, ${who}
	 */
	@GetMapping("/hello")
	public String sayHello(@RequestParam(required = false, name = "name") String name) {
		if (StrUtil.isBlank(name)) {
			name = "whcoding";
		}
		return StrUtil.format("Hello World, {}!", name);
	}

	/**
	 * 返回JSON
	 *
	 * @param name
	 * @return
	 */
	@GetMapping("/sayHelloStr")
	public String sayHelloStr(@RequestParam String name) {
		Map<String, String> map = new HashMap<>();
		map.put("customerName", "西安客户");
		map.put("customerNo", "XADC");
		map.put("customerID", "123456");
		map.put("testName", name);
		String jsonStr = JSONUtil.toJsonStr(map);
		return jsonStr;
//		return StrUtil.format("Hello World, {}!", name);
	}

	/**
	 * 参数转为Map
	 *
	 * @param jsonStr
	 * @return
	 */
	@PostMapping("/paramByJSONTest")
	public String paramByJSONTest(@RequestParam String jsonStr) {
		Map<String, String> map = new HashMap<>();
		map.put("customerName", "西安客户");
		map.put("customerNo", "XADC");
		map.put("customerID", "123456");
		JSONObject jsonObject = JSONUtil.parseObj(jsonStr);
		Map testMap = JSONUtil.toBean(jsonObject, Map.class);
		return jsonObject.toString();
	}

	/**
	 * 参数转为Map
	 *
	 * @param JSONStr
	 * @return
	 */
	@PostMapping("/paramByJSONTest2")
	public String paramByJSONTest2(@RequestParam String JSONStr) {
		return paramByJSONTest(JSONStr);
	}

}
